﻿using System.Collections.Generic;
using System.Web.Script.Serialization;
using Taype.Trading.BTC.Utilities;
using System;

namespace Taype.Trading.BTC.MtGoxAPIClient.JSONObjects
{
    /// <summary>
    /// This class encaspulates an open order on the MtGox platform
    /// </summary>
    public class MtGoxOrder
    {

        public string oid { get; set; }

        public MtGoxCurrencySymbol currency { get; set; }
            
        public MtGoxCurrencySymbol item { get; set; }
        
        public MtGoxOrderType type  { get; set; }
            
        public double amount { get; set; }
        
        public Int64 amount_int { get; set; }
            
        public double price { get; set; }
        
        public Int64 price_int { get; set; }
            
        public MtGoxOrderStatus status { get; set; }
        
        public string real_status { get; set; }
            
        public int dark { get; set; }
            
        public int date { get; set; }
        
        public Int64 priority { get; set; }

        /// <summary>
        ///  Parse JSON data returned by the 0/buyBTC.php and 0/sellBTC.php methods
        ///  // if there are no open orders json data will look like this:
        /// "{\"usds\":\"0\",\"btcs\":\"0\",\"new_usds\":{\"value\":\"0.00000\",\"value_int\":\"0\",\"display\":\"$0.00000\",\"currency\":\"USD\"},\"new_btcs\":{\"value\":\"0.00000000\",\"value_int\":\"0\",\"display\":\"0.00000000\\u00a0BTC\",\"currency\":\"BTC\"},\"orders\":[]}"
        /// // if there are open orders json data will look like this:
        /// "{\"status\":\"Your order has been queued for execution\",\"oid\":\"626c07b9-6f58-4665-b972-0c3f438b6ab4\",\"orders\":[{\"oid\":\"626c07b9-6f58-4665-b972-0c3f438b6ab4\",\"currency\":\"USD\",\"item\":\"BTC\",\"type\":2,\"amount\":\"0.01000000\",\"amount_int\":\"1000000\",\"price\":\"1.00000\",\"price_int\":\"100000\",\"status\":2,\"real_status\":\"pending\",\"dark\":0,\"date\":1325790759,\"priority\":\"1325790759344099\"}]}"
        /// </summary>
        /// <param name="jsonDataStr">JSON data returned by the 0/buyBTC.php and 0/sellBTC.php methods</param>
        /// <returns></returns>
        public static List<MtGoxOrder> getObjects(string jsonDataStr)
        {
            List<MtGoxOrder> orderList = new List<MtGoxOrder>();
            string json = jsonDataStr;
            var serializer = new JavaScriptSerializer();
            serializer.RegisterConverters(new[] { new DynamicJsonConverter() });
            dynamic obj = serializer.Deserialize(json, typeof(object));
            // "{\"status\":\"Your order has been queued for execution\",
            if (obj.status == null)
            { 
                // no open orders
                // return null;
            }
            else
            {
                // there are open orders
            }
            for (int i = 0; i < obj.orders.Count; i++)
            {
                MtGoxOrder ord = new MtGoxOrder();
                // \"oid\":\"626c07b9-6f58-4665-b972-0c3f438b6ab4\",
                ord.oid = obj.orders[i].oid.ToString();
                // \"currency\":\"USD\",
                ord.currency = (MtGoxCurrencySymbol)Enum.Parse(typeof(MtGoxCurrencySymbol), obj.orders[i].currency.ToString(), true);
                // \"item\":\"BTC\",
                ord.item = (MtGoxCurrencySymbol)Enum.Parse(typeof(MtGoxCurrencySymbol), obj.orders[i].item.ToString(), true);
                // \"type\":2,
                switch ((int)int.Parse(obj.orders[i].type.ToString()))
                {
                    case 1:
                        ord.type = MtGoxOrderType.Sell;
                        break;
                    case 2:
                        ord.type = MtGoxOrderType.Buy;
                        break;
                }
                // \"amount\":\"0.01000000\",
                ord.amount = double.Parse(obj.orders[i].amount.ToString());
                // \"amount_int\":\"1000000\",
                ord.amount_int = Int64.Parse(obj.orders[i].amount_int.ToString());
                // \"price\":\"1.00000\",
                ord.price = double.Parse(obj.orders[i].price.ToString());
                // \"price_int\":\"100000\",
                ord.price_int = Int64.Parse(obj.orders[i].price_int.ToString());
                // \"status\":2,
                switch ((int)int.Parse(obj.orders[i].status.ToString()))
                {
                    case 0:
                        ord.status = MtGoxOrderStatus.Invalid;
                        break;
                    case 2:
                        ord.status = MtGoxOrderStatus.Invalid;
                        break;
                }
                //ord.status = int.Parse(obj.orders[i].status.ToString());
                // \"real_status\":\"pending\",
                ord.real_status = obj.orders[i].real_status.ToString();
                // \"dark\":0,
                ord.dark = int.Parse(obj.orders[i].dark.ToString());
                // \"date\":1325790759,
                ord.date = int.Parse(obj.orders[i].date.ToString());
                // \"priority\":\"1325790759344099\"}]}"
                ord.priority = Int64.Parse(obj.orders[i].priority.ToString());
                orderList.Add(ord);
            }
            return orderList;
        }
    }
}
